//
//  ZCXPlayerViewController.m
//  LoveOfPet
//
//  Created by dllo on 15/11/23.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "ZCXPlayerViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <SVProgressHUD.h>
#define HEIGHT  [[UIScreen mainScreen]bounds].size.height / 667
#define WIDTH   [[UIScreen mainScreen]bounds].size.width / 375
@interface ZCXPlayerViewController ()
@property (nonatomic, retain) AVPlayer *player;
@property (nonatomic, assign) BOOL fun;
@end

@implementation ZCXPlayerViewController
- (void)dealloc{
    [_player release];
    [_mvModel release];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self playVideo];
    
}
- (void)playVideo{
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 * HEIGHT)];
    backView.userInteractionEnabled = YES;
    [self.view addSubview:backView];
    self.player = [AVPlayer playerWithURL:[NSURL URLWithString:self.mvModel.video]];
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
    playerLayer.backgroundColor = [UIColor blackColor].CGColor;
    playerLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, backView.frame.size.height);
    [backView.layer addSublayer:playerLayer];
    [self.player play];
    [backView release];
    UITapGestureRecognizer *imageTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushIntoUserDetial:)];
    [backView addGestureRecognizer:imageTap];
    [imageTap release];
    [self addNotification];
}
-(void)addNotification
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(playDidFinish) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
}
-(void)playDidFinish
{
//    NSLog(@"播放完成");
    [self playVideo];
}
- (void)pushIntoUserDetial:(UIGestureRecognizer *)tap{
    if (self.fun == NO) {
        [_player pause];
        self.fun = YES;
    }else{
        [_player play];
        self.fun = NO;
    }
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [SVProgressHUD dismiss];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
